#pragma once

#include <QObject>
#include <QString>
#include <QHash>
#include <QList>
#include <functional>
#include "kernel/domain/map_types.h"

namespace application {
namespace area {

/**
 * @brief 区域管理类
 * @details 管理地图上的区域占用状态，支持AGV进入/退出区域的跟踪
 */
class Area : public QObject {
    Q_OBJECT

public:
    explicit Area(const QString& areaId, QObject* parent = nullptr);
    ~Area() = default;

    // 区域基本信息
    QString getAreaId() const { return m_areaId; }
    void setBounds(const kernel::Position& topLeft, const kernel::Position& bottomRight);

    // 占用管理
    bool isOccupied() const;
    bool isOccupiedBy(int agvId) const;
    bool occupy(int agvId);
    bool releaseOccupation(int agvId);

    // 区域进入/退出
    void enterArea(int agvId);
    void exitArea(int agvId);

    // 查询
    QList<int> getOccupyingAgvIds() const;
    int getOccupyingAgvCount() const { return m_occupyingAgvIds.size(); }
    bool containsPoint(const kernel::Position& position) const;

signals:
    void areaEntered(int agvId, const QString& areaId);
    void areaExited(int agvId, const QString& areaId);
    void occupationChanged(bool occupied);

private:
    QString m_areaId;
    kernel::Position m_topLeft;
    kernel::Position m_bottomRight;

    // 当前在区域内的AGV列表
    QHash<int, int> m_occupyingAgvIds;  // AGV ID -> 引用计数
    bool m_isOccupied = false;
};

} // namespace area
} // namespace application
